<template>
  <div class="permission-container">
    <div class="app-container">
      <PageTools>
        <template #right>
          <el-button
            size="small"
            type="primary"
            @click="showDialog('1', '0')"
          >添加权限</el-button>
        </template>
      </PageTools>
      <el-table border :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" align="center" />
        <el-table-column label="描述" prop="description" align="center" />
        <el-table-column label="操作" align="center">
          <template #default="{ row }">
            <el-button
              type="text"
              @click="showDialog('2', row.id)"
            >添加</el-button>
            <el-button type="text" @click="edit(row)">编辑</el-button>
            <el-button type="text" @click="del(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <permission-dialog
      ref="permissionDialogRef"
      :visible="isShow"
      :type="type"
      :pid="pid"
      @close="closeFn"
      @upload="fetchPermissionList"
    />
  </div>
</template>

<script>
import PageTools from '@/components/PageTools/index.vue'
import { getPermissionList, delPermission } from '@/api'
import findSon from '@/utils/findSon'
import PermissionDialog from './components/permission-dialog.vue'
export default {
  components: { PageTools, PermissionDialog },
  data() {
    return {
      list: [],
      isShow: false,
      type: '',
      pid: ''
    }
  },
  created() {
    this.fetchPermissionList()
  },
  methods: {
    // 获取数据
    async fetchPermissionList() {
      const res = await getPermissionList()
      console.log(res)
      this.list = findSon(res.data, '0')
      console.log(this.list)
    },
    // 关闭弹窗
    closeFn() {
      this.isShow = false
    },
    // 打开dialog弹窗
    showDialog(type, pid) {
      this.isShow = true
      this.type = type
      this.pid = pid
    },
    // 编辑
    edit(row) {
      this.isShow = true
      this.$refs.permissionDialogRef.getDetail(row.id)
    },
    // 删除
    del(row) {
      this.$confirm('此操作将永久删除该权限点, 确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 2. 调用删除接口
        await delPermission(row.id)
        // 3. 提示用户
        this.$message.success('删除权限点成功')
        // 4. 刷新列表
        this.fetchPermissionList()
      }).catch(() => {
        this.$message.info('已取消该操作')
      })
    }
  }
}
</script>
<style lang='scss' scoped>
.el-table {
  margin-top: 20px;
}
</style>
